clear;clc;

% =========================================================================
% 0. LOAD DATA
% =========================================================================


% 1 No FinTech
load NoFin_pf75_x75.mat
irf_refi_yp = irf_adj_yp.*(irf_b1_yp>BINI);
irf_refi_ypr = irf_adj_ypr.*(irf_b1_ypr>BINI);

refi_bf_nofin  = mean(irf_refi_yp(:,1));
refi_af_nofin  = mean(irf_refi_ypr(:,1));
refi_dif_nofin = mean(irf_refi_ypr(:,1))-mean(irf_refi_yp(:,1));

c_bf_nofin  = mean(irf_c_yp(:,1));
c_af_nofin  = mean(irf_c_ypr(:,1));
c_dif_nofin = mean(log(irf_c_ypr(:,1))-log(irf_c_yp(:,1)));

% 2 FinTech w.o. spillovers
load Fin_pq7_q5_pf75_x75.mat

irf_refi_yp = irf_adj_yp.*(irf_b1_yp>BINI);
irf_refi_ypr = irf_adj_ypr.*(irf_b1_ypr>BINI);

refi_bf_fin  = mean(irf_refi_yp(:,1));
refi_af_fin  = mean(irf_refi_ypr(:,1));
refi_dif_fin = mean(irf_refi_ypr(:,1))-mean(irf_refi_yp(:,1));

c_bf_fin  = mean(irf_c_yp(:,1));
c_af_fin  = mean(irf_c_ypr(:,1));
c_dif_fin = mean(log(irf_c_ypr(:,1))-log(irf_c_yp(:,1)));

ms_bf_fin  = sum(irf_refi_yp.*(repmat(qini,1,T)==q|repmat(eini,1,T)==e))./sum(irf_refi_yp);
ms_af_fin  = sum(irf_refi_ypr.*(repmat(qini,1,T)==q|repmat(eini,1,T)==e))./sum(irf_refi_ypr);
ms_dif_fin = ms_af_fin-ms_bf_fin;


% 3 FinTech w. spillovers (baseline)
load FinE_pq7_q5_pe6_e8.5_pf75_x75.mat

irf_refi_yp = irf_adj_yp.*(irf_b1_yp>BINI);
irf_refi_ypr = irf_adj_ypr.*(irf_b1_ypr>BINI);


refi_bf_efin  = mean(irf_refi_yp(:,1));
refi_af_efin  = mean(irf_refi_ypr(:,1));
refi_dif_efin = mean(irf_refi_ypr(:,1))-mean(irf_refi_yp(:,1));

c_bf_efin  = mean(irf_c_yp(:,1));
c_af_efin  = mean(irf_c_ypr(:,1));
c_dif_efin = mean(log(irf_c_ypr(:,1))-log(irf_c_yp(:,1)));

ms_bf_efin  = sum(irf_refi_yp.*(repmat(qini,1,T)==q|repmat(eini,1,T)==e))./sum(irf_refi_yp);
ms_af_efin  = sum(irf_refi_ypr.*(repmat(qini,1,T)==q|repmat(eini,1,T)==e))./sum(irf_refi_ypr);
ms_dif_efin = ms_af_efin-ms_bf_efin;

% =========================================================================
% Model Figure 1
% =========================================================================

cat = categorical({'No-FinTech','No-spillover','Baseline'});
cat = reordercats(cat,{'No-FinTech','No-spillover','Baseline'});

% (I) Impact refi and C response
figure;
subplot(2,2,1)
b=bar(cat,100*[[refi_bf_nofin refi_bf_fin refi_bf_efin];...
               [refi_af_nofin refi_af_fin refi_af_efin]]);
ylim([0 20])
title('(a) Refi propensity')
legend('Before MP shock','After MP shock')
legend boxoff 
ylabel('Level (%)')

subplot(2,2,2)
plot(cat,100*[refi_dif_nofin refi_dif_fin refi_dif_efin]','-s','LineWidth',1.5,'Color','k')
ylim([9 12])
title('(b) Refi-propensity response')
ylabel('Change (pp)')

subplot(2,2,3)
b=bar(cat,[[c_bf_nofin c_bf_fin c_bf_efin];...
         [c_af_nofin c_af_fin c_af_efin]]);
ylim([0.89 0.905])
legend('Before MP shock','After MP shock')
legend boxoff 
title('(c) Consumption')
ylabel('Level')

subplot(2,2,4)
plot(cat,100*[c_dif_nofin c_dif_fin c_dif_efin],'-s','LineWidth',1.5,'Color','r')
ylim([1 1.3])
title('(d) Consumption response')
ylabel('% Change')

